<?php

require_once(WSB_MODULES_PATH . 'cms/models/Pages.php');
require_once(WSB_MODULES_PATH . 'cms/models/Page.php');

class Cms_Model_DbTable_Page extends WSB_Db_Table
{

    protected $_name = 'page';
    protected $_primary = 'id';
    protected $_rowsetClass = 'Cms_Model_Pages';
    protected $_rowClass = 'Cms_Model_Page';
    protected $_dependentTables = array();
	protected $_order = 'iso ASC';
	protected $_listcols = array('id', 'iso', 'language');

	/**
	 * @return SimpleXMLElement
	 */
	public function getSystemLanguages()
	{
        $path = WSB_APP_PATH . 'i18n/languages.xml';
        if(is_readable($path)) {
            $xml = simplexml_load_file($path);
            if($xml instanceof SimpleXMLElement) {
                return $xml;
            }
        }
	}

	/**
	 * @param integer $pid
	 */
	public function reorderPages($pid)
	{
		$adapter = $this->getAdapter();
		$data = array(
			'sort' => WSB_Db_Table::SORT_DELETED
		);
		$where = array(
			$adapter->quoteInto('deleted=?', 1),
			$adapter->quoteInto('sort<>?', WSB_Db_Table::SORT_DELETED)
		);
		$this->update($data, $where);

		$i = 0;
		$where = array(
			$adapter->quoteInto('deleted=?', 0),
			$adapter->quoteInto('pid<>?', $pid)
		);
		foreach($this->fetchAll($where, 'sort ASC') AS $item) {
			if($i != $item->sort) {
				$item->sort = $i;
				$item->save();
			}
			$i++;
		}
	}

	/**
	 * @param integer $pid
	 * @param integer $sort
	 * @return integer
	 */
	public function addPage($pid, $sort)
	{

		// @todo: Zend_Db_Table einsetzen anstatt SQL schreiben
		$sql  = 'UPDATE page';
		$sql .= ' SET sort=sort+1';
		$sql .= ' WHERE pid=' . $pid;
		$sql .= ' AND sort>=' . $sort;
    	$this->getAdapter()->query($sql);

    	$item = $this->createRow();
    	$item->pid = $pid;
    	$item->title = '**Neue Seite**';
    	$item->sort = $sort;
    	$item->shortcut = 0;
		$item->shortcut_page_id = 0;
		$item->template_id = 0;
		$item->template_not_recursive = 0;
		$item->nav_hide = 0;
		$item->hidden = 0;
		$item->deleted = 0;
    	$item->save();

		$this->reorderPages($pid);

		return $item->id;
	}

	/**
	 * Einfuegen (nach Ausschneiden einer Seite)
	 *
	 * @param integer $cutIdPage
	 * @param integer $pid
	 * @param integer $sort
	 */
	public function pastePage($cutIdPage, $pid, $sort)
	{

		$where = $this->getAdapter()->quoteInto('id=?', $cutIdPage);
		$cuttedPage = $this->fetchRow($where);

		// @todo: Zend_Db_Table einsetzen anstatt SQL schreiben
		$sql  = 'UPDATE page';
		$sql .= ' SET sort=sort+1';
		$sql .= ' WHERE pid=' . $pid;
		$sql .= ' AND sort>=' . $sort;
    	$test = $this->getAdapter()->query($sql);

    	//$cuttedPage->id = $cutIdPage;
    	$cuttedPage->pid = $pid;
    	$cuttedPage->sort = $sort;
    	$cuttedPage->save();

		$this->reorderPages($pid);
		$this->reorderPages($cuttedPage->pid);

	}

	/**
	 * @param integer $page_id
	 * @param integer $pid
	 */
	public function deletePage($page_id, $pid)
	{
		$where = $this->getAdapter()->quoteInto('id=?', $page_id);
		$item = $this->fetchRow($where);
		$item->delete();
		$this->reorderPages($pid);
	}

}